package com.ybkj.o2o.mc.controller.res;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.mc.common.transport.Error;
import com.ybkj.o2o.mc.common.transport.Result;
import com.ybkj.o2o.mc.entity.Passenger;
import com.ybkj.o2o.mc.entity.PassengerAddress;
import com.ybkj.o2o.mc.manager.PassengerAddressManager;
import com.ybkj.o2o.mc.manager.PassengerManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "api/passengers")
public class PassengerAddressRes {
  @Autowired
  private PassengerAddressManager passengerAddressManager;
  @Autowired
  private PassengerManager passengerManager;

  /**
   * 创建常用目的地.
   */
  @RequestMapping(value = {"{passengerId}/addressbooks"}, method = RequestMethod.POST)
  public ResponseEntity<Object> create(
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("address") String address,
          @RequestParam(required = false, value = "addressDetail") String addressDetail,
          @RequestParam("latitude") Double latitude,
          @RequestParam("longitude") Double longitude,
          @RequestParam("type") Integer type,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    address = StringUtils.trimToEmpty(address);
    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(address), "目的地不能为空");
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    //判断是否存在相同
    boolean exists = passengerAddressManager.exists(address, type, passengerId);

    Preconditions.checkArgument(!exists, "此地址已经存在");

    //保存
    try {

      PassengerAddress passengerAddress = new PassengerAddress();
      passengerAddress.setAddress(address);
      passengerAddress.setAddressDetail(addressDetail);
      passengerAddress.setPassengerId(passengerId);
      passengerAddress.setLatitude(latitude);
      passengerAddress.setLongitude(longitude);
      passengerAddress.setType(type);

      passengerAddress = passengerAddressManager.insert(passengerAddress);

      Result<PassengerAddress> result = new Result<>(0, "成功", passengerAddress);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 删除乘客的常用目的地.
   */
  @RequestMapping(value = "{passengerId}/addressbooks/{addressId}", method = RequestMethod.DELETE)
  public ResponseEntity<Object> delete(
          @PathVariable("addressId") Long addressId,
          @PathVariable("passengerId") Long passengerId,
          @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;

    appKey = StringUtils.trimToEmpty(appKey);
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(addressId != null && addressId != 0, "目的地Id不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");

    //删除
    try {
      passengerAddressManager.delete(addressId, passengerId);

      Result<PassengerAddress> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.NO_CONTENT);

      return responseEntity;

    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());
      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);
      return responseEntity;
    }

  }

  /**
   * 修改乘客的常用目的地.
   */
  @RequestMapping(value = "{passengerId}/addressbooks/{addressId}", method = RequestMethod.PUT)
  public ResponseEntity<Object> update(@PathVariable("addressId") Long addressId,
                                       @PathVariable("passengerId") Long passengerId,
                                       @RequestParam("address") String address,
                                       @RequestParam("latitude") Double latitude,
                                       @RequestParam("longitude") Double longitude,
                                       @RequestParam("type") Integer type,
                                       @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;

    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Preconditions.checkArgument(addressId != null && addressId != 0, "目的地Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    //判断是否存在
    PassengerAddress passengerAddress = passengerAddressManager.find(addressId, passengerId);
    Preconditions.checkArgument(address != null, "联系人不存在");

    try {
      passengerAddress.setAddress(address);
      passengerAddress.setLatitude(latitude);
      passengerAddress.setLongitude(longitude);
      passengerAddress.setType(type);

      passengerAddressManager.update(passengerAddress);
      Result<Passenger> result = new Result<>(0, "成功", null);

      responseEntity = new ResponseEntity<>(result, HttpStatus.CREATED);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 查询乘客常用目的地.
   *
   * @return 一个常用目的地
   */
  @RequestMapping(value = {"{passengerId}/addressbooks"}, method = RequestMethod.GET)
  public ResponseEntity<Object> findConcats(@PathVariable("passengerId") Long passengerId,
                                            @RequestParam("type") Integer type,
                                            @RequestParam("appKey") String appKey) {

    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    try {
      PassengerAddress adress = passengerAddressManager.findByPassengerAndType(passengerId, type);

      Result<PassengerAddress> result = new Result<>(0, "成功", adress);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

  /**
   * 查询客户全部常用目的地.
   */
  @RequestMapping(value = {"queryAddress"}, method = RequestMethod.GET)
  public ResponseEntity<Object> queryAddress(
          @RequestParam("passengerId") Long passengerId,
          @RequestParam("appKey") String appKey) {
    ResponseEntity<Object> responseEntity = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(passengerId != null && passengerId != 0, "乘客Id不能为空");
    Passenger passenger = passengerManager.findByIdAndAppKey(passengerId, appKey);
    Preconditions.checkArgument(passenger != null, "此乘客不存在");
    try {
      List<PassengerAddress> list = passengerAddressManager.findByPassenger(passengerId);

      Result<List<PassengerAddress>> result = new Result<>(0, "成功", list);

      responseEntity = new ResponseEntity<>(result, HttpStatus.OK);

      return responseEntity;
    } catch (Exception ex) {
      ex.printStackTrace();
      Error error = new Error(500, "内部服务器错误：" + ex.getMessage());

      responseEntity = new ResponseEntity<>(error, HttpStatus.INTERNAL_SERVER_ERROR);

      return responseEntity;
    }
  }

}
